import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/view_model/academic_sentiment/knowledge_statistics_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///用户： ninetailedfox
///时间： 2020/7/4
///邮箱： 1037438704@qq.com
///功能： 知识点分布

class KnowledgeStatisticsPage extends StatefulWidget {
  @override
  _KnowledgeStatisticsPageState createState() =>
      _KnowledgeStatisticsPageState();
}

class _KnowledgeStatisticsPageState extends State<KnowledgeStatisticsPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<KnowledgeStatisticsViewModel>(
      onModelReady: (value) => value.initView(),
      model: KnowledgeStatisticsViewModel(context),
      builder: (context, viewModel, child) {
        return Scaffold(
          appBar: MyAppBar2(title: "知识点分布"),
          body: viewModel.nodesList == null
              ? Center(child: CircularProgressIndicator())
              : (viewModel.myAppViewModel.identity
                  ? itemList(
                      viewModel
                          .myAppViewModel.studentInformationModel.className,
                      viewModel.nodesList)
                  : ListView.builder(
                      itemCount: viewModel.nodesList.length,
                      itemBuilder: (context, index) {
                        Map<String, dynamic> listNodes =
                            viewModel.nodesList[index];
                        List gridList = listNodes["nodes"];
                        return itemList(listNodes["subject"], gridList);
                      })),
        );
      },
    );
  }

  Widget itemList(String titleText, List gridList) {
    if (gridList == null) {
      return Container();
    }
    return Column(
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(35),
            top: ScreenUtil().setWidth(35),
            right: ScreenUtil().setWidth(35),
          ),
          child: Text(titleText ?? "未获取到", style: TextStyles.textDark43),
        ),
        gridList == null
            ? Container()
            : Padding(
                padding: EdgeInsets.all(ScreenUtil().setWidth(35)),
                child: Material(
                  elevation: 1,
                  borderRadius: BorderRadius.circular(10),
                  child: Container(
                    decoration:
                        BoxDecoration(borderRadius: BorderRadius.circular(10)),
                    child: GridView.builder(
                      shrinkWrap: true,
                      itemCount: gridList.length,
                      physics: NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.all(16),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3,
                        mainAxisSpacing: 5,
                        crossAxisSpacing: 5,
                        childAspectRatio: 1,
                      ),
                      itemBuilder: (context, index) {
                        Map<String, dynamic> mapItem = gridList[index];
                        double score = 0.1;
                        if (mapItem["score"] != null) {
                          score = double.parse(mapItem["score"]);
                        }
                        return GradientCircularProgressRoute(
                          endDouble: score,
                          student: mapItem["name"],
                        );
                      },
                    ),
                  ),
                ),
              ),
      ],
    );
  }
}
